package com.xiguthings.xiniu.iot.device.state.jedis.config;

import com.xiguthings.xiniu.iot.device.state.jedis.JedisClient;
import com.xiguthings.xiniu.iot.device.state.jedis.impl.JedisClientCluster;
import com.xiguthings.xiniu.iot.device.state.jedis.impl.JedisClientPool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

@Configuration
public class JedisConfig {
    //    @Value("${Jedis.client.class.name}")
//    private String jedisClientClassName;
    @Value("${redis_hostports}")
    private String redisHostAndPort;
    @Value("${redis_password}")
    private String redisPassword;
    @Value("${redis.connect.maxTotal}")
    private int maxTotal;
    @Value("${redis.connect.maxWaitMillis}")
    private int maxWaitMillis;
    @Value("${redis.connect.maxIdle}")
    private int maxIdle;


    @Bean
    public JedisClient jedisClient() throws Exception {
        JedisClient jedisClient = null;
        String[] addrs = redisHostAndPort.split(",");
        if (addrs.length > 1) {
            JedisClientCluster jedisClientCluster = new JedisClientCluster();// 集群
            jedisClientCluster.setJedisCluster(jedisCluster());
            jedisClient = jedisClientCluster;
        } else {
            JedisClientPool jedisClientPool = new JedisClientPool();// 单机
            jedisClientPool.setJedisPool(jedisPool());
            jedisClient = jedisClientPool;
        }
        return jedisClient;
    }


    private JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        if (maxTotal == 0) {
            maxTotal = 100;
        }
        if (maxWaitMillis == 0) {
            maxWaitMillis = 1000;
        }
        if (maxIdle == 0) {
            maxIdle = 10;
        }
        // 设置最大连接数
        jedisPoolConfig.setMaxTotal(maxTotal);
        // 设置最大阻塞时间，记住是毫秒数milliseconds
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 设置空闲连接
        jedisPoolConfig.setMaxIdle(maxIdle);
        return jedisPoolConfig;
    }

    private JedisCluster jedisCluster() {
        Set<HostAndPort> nodes = new HashSet<>();
        String[] hostAndportArr = redisHostAndPort.split(",");
        for (String hostAndPortStr : hostAndportArr) {
            String redisHost = hostAndPortStr.split(":")[0];
            Integer redisPort = new Integer(hostAndPortStr.split(":")[1]);
            HostAndPort hostAndPort = new HostAndPort(redisHost, redisPort);
            nodes.add(hostAndPort);
        }
        JedisCluster jedisCluster = new JedisCluster(nodes, 10000, 10000, 3, redisPassword, jedisPoolConfig());
        return jedisCluster;
    }

    private JedisPool jedisPool() {
        String[] hostAndportArr = redisHostAndPort.split(",");
        String redisHost = hostAndportArr[0].split(":")[0];
        Integer redisPort = new Integer(hostAndportArr[0].split(":")[1]);
        if (StringUtils.isBlank(redisPassword)) {
            redisPassword = null;
        }
        JedisPool jedisPool = new JedisPool(jedisPoolConfig(), redisHost, redisPort, 10000, redisPassword);
        return jedisPool;
    }

}
